package com.tcxhb.job.dao.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tcxhb.job.dao.dataobject.JobMachineDO;
import com.tcxhb.job.dao.dataobject.query.JobMachineQuery;
import com.tcxhb.job.dao.mapper.JobMachineMapper;
import com.tcxhb.job.dao.repository.JobMachineRepository;
import com.tcxhb.job.common.model.PageResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

/**
 * author:auto.generator
 * time: 2023-11-05
 */
@Repository
public class JobMachineRepositoryImpl extends ServiceImpl<JobMachineMapper, JobMachineDO> implements JobMachineRepository {

    @Override
    public Long create(JobMachineDO jobMachineDO) {
        super.save(jobMachineDO);
        return jobMachineDO.getId();
    }

    @Override
    public boolean deleteById(Long id) {
        return super.removeById(id);
    }

    @Override
    public boolean updateById(JobMachineDO jobMachineDO) {
        return super.updateById(jobMachineDO);
    }

    @Override
    public JobMachineDO queryById(Long id) {
        return super.getById(id);
    }

    @Override
    public PageResponse<JobMachineDO> page(JobMachineQuery query) {
        LambdaQueryWrapper<JobMachineDO> wrapper = queryWrapper(query);
        Page<JobMachineDO> page = new Page<>(query.getPage().getPageNum(), query.getPage().getPageSize());
        IPage response = page(page, wrapper);
        PageResponse result = new PageResponse();
        result.setTotal(response.getTotal());
        result.setList(response.getRecords());
        return result;
    }

    @Override
    public List<JobMachineDO> list(JobMachineQuery query) {
        LambdaQueryWrapper<JobMachineDO> wrapper = queryWrapper(query);
        if (query.getPage() != null) {
            wrapper.last(" limit " + query.getPage().getPageSize());
        }
        return super.list(wrapper);
    }

    @Override
    public boolean updateByHost(JobMachineDO machineDO) {
        UpdateWrapper<JobMachineDO> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("ip", machineDO.getIp());
        updateWrapper.eq("port", machineDO.getPort());
        updateWrapper.eq("app_name", machineDO.getAppName());
        updateWrapper.set("status", machineDO.getStatus());
        updateWrapper.set("update_time", new Date());
        return super.update(updateWrapper);
    }

    private LambdaQueryWrapper<JobMachineDO> queryWrapper(JobMachineQuery query) {
        LambdaQueryWrapper<JobMachineDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(query.getId() != null, JobMachineDO::getId, query.getId());
        if (StringUtils.isNotBlank(query.getIp())) {
            wrapper.eq(JobMachineDO::getIp, query.getIp());
        }
        //id 翻页
        if (query.getLastId() != null) {
            wrapper.gt(JobMachineDO::getId, query.getLastId());
        }
        //心跳时间大于
        if (query.getBeatTimeStart() != null) {
            wrapper.ge(JobMachineDO::getBeatTime, query.getBeatTimeStart());
        }
        //心跳时间小于
        if (query.getBeatTimeEnd() != null) {
            wrapper.le(JobMachineDO::getBeatTime, query.getBeatTimeEnd());
        }
        wrapper.eq(query.getPort() != null, JobMachineDO::getPort, query.getPort());
        wrapper.eq(query.getStatus() != null, JobMachineDO::getStatus, query.getStatus());
        if (StringUtils.isNotBlank(query.getAppName())) {
            wrapper.eq(JobMachineDO::getAppName, query.getAppName());
        }
        //根据更新时间
        if ("id".equals(query.getAscKey())) {
            wrapper.orderByAsc(JobMachineDO::getId);
        } else {
            wrapper.orderByDesc(JobMachineDO::getUpdateTime);
        }
        return wrapper;
    }
}
